﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqData;

public partial class Add_Color : System.Web.UI.Page
{

    IDataRepository _dataObject = new DataRepository();

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Request.QueryString["Id"] != null)
            {
                lblHeader.Text = "Edit Color";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add Color";
            }
        }

    }
    #endregion

    #region save cancel and clear
    protected void Save(object sender, EventArgs e)
    {
        Color Color = new Color();
        Color getColor = new Color();
        try
        {
            if (string.IsNullOrEmpty(txtColor.Text.Trim()))
            {
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('* are mendatory fields.')", true);
                return;
            }
            Color = new Color(_dataObject);
            BuildObject(Color);

            //check Color name is exist or not
            getColor = new Color(_dataObject);
            BuildColorObject(getColor);

            if (Request.QueryString["Id"] != null)
            {
                if (getColor.Id != Convert.ToInt32(Request.QueryString["Id"]) && getColor.Id != 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Color Name is already exist..')", true);
                    return;
                }
                Color.Id = Convert.ToInt32(Request.QueryString["Id"]);
                Color.UpdatedDate = System.DateTime.Now;
                Color.IsDeleted = false;
                Color.Update();
                Response.Redirect("Color-List.aspx");
            }
            else
            {
                if (getColor.Id > 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Color Name is already exist..')", true);
                    return;
                }
                BuildObject(Color);
                Color.CreatedDate = System.DateTime.Now;
                Color.Create();
                ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "myscript", "alert('Color is added successfully');", true);
            }
            ClearControls();

        }
        catch (Exception ex)
        {
        }
    }

    private void ClearControls()
    {
        txtColor.Text = string.Empty;

    }
    #endregion


    #region binddata
    private void BindData()
    {
        Color color = new Color();
        color = new Color(_dataObject);
        color.Id = Convert.ToInt32(Request.QueryString["Id"]);
        color.GetColorById();
        txtColor.Text = color.Name;
    }
    private void BuildObject(Color Color)
    {
        Color.Name = txtColor.Text;
    }
    private void BuildColorObject(Color Color)
    {
        Color.Name = txtColor.Text;
        Color.GetColorByName();
    }

    #endregion
}